package DAO;


import Entity.InterTab;
import Entity.Manger;
import Entity.scTab;
import util.Result;
import util.RfidTool;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MangerDao {
    private static short tsector = 1;
    private static short tblk = 6;

    static Connection connect = null;//数据库连接对象
    static PreparedStatement cmmd = null;//数据库命令执行对象
    static ResultSet rs = null;//数据集
    static String sql = "";


    RfidTool instance = RfidTool.getInstance();
    String cardID = instance.showCardId();//获取id卡的号码


    public String msg() {
        String msg = instance.getdeviceInti();
        System.out.println(msg);
        return msg;
    }

    public Result insert(Manger in) {
        Result result = new Result();

        sql = "insert into Mannger values(?,?,?,?,?,?,?)";
        try {

            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);

            cmmd.setString(1, in.getID());
            cmmd.setString(2, in.getName());//传入参数的个数
            cmmd.setString(3, in.getAccount());
            cmmd.setString(4, in.getPassword());
            cmmd.setString(5, in.getInterID());
            cmmd.setString(6, in.getInterName());
            cmmd.setInt(7, in.isSenior());
            cmmd.execute(); //执行sql语句
            System.out.println(cmmd);
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }


    public Result update3(Manger in) {
    Result result = new Result();
        sql = "update Mannger set Name = '" + in.getName() + "',Account = '" + in.getAccount() + "',Password = '" + in.getPassword() + "',InterID= '" + in.getInterID() + "',InterName = '" + in.getInterName() + "',IsSenior = '" + in.isSenior() + "'  where ID = '" + in.getID() + "'";
        try {
            System.out.println(sql);
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            cmmd.execute();

        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;

    }

    public Result Delete(String out) {
  Result result =new Result();
        sql = "delete from Mannger where ID='"+out+"'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            cmmd.execute(); //执行sql语句
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        return result;
    }


    public Manger Querry_login(String x) {
        Manger uu = new Manger();

        sql = " select count(*) from Mannger where Account = ?";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            cmmd.setString(1, x);
            rs = cmmd.executeQuery();
            while (rs.next()) {
                if (rs.getInt(1) == 0) {
                    JOptionPane.showMessageDialog(null, "对不起此人不存在");
                    break;
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        sql = "Select * from Mannger where Account = ?";

        try {

            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            cmmd.setString(1, x);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                uu.setID(rs.getString(1));
                uu.setName(rs.getString(2));
                uu.setAccount(rs.getString(3));
                uu.setPassword(rs.getString(4));
                uu.setInterID(rs.getString(5));
                uu.setInterName(rs.getString(6));
                uu.setSenior(rs.getInt(7));
            }

            if (x != null) {
                return uu;
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(null, "此人不存在");
        }


        return uu;
    }


    public Result Querry(String x) {
        Manger mannger;
        Result result = new Result();
        List<Manger> list = new ArrayList<>();
        sql = "Select * from Mannger where  ID like '%" + x + "%' order by ID+''";

        try {

            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            // cmmd.setString(1,x);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                mannger = new Manger();
                mannger.setID(rs.getString(1));
                mannger.setName(rs.getString(2));
                mannger.setAccount(rs.getString(3));
                mannger.setPassword(rs.getString(4));
                mannger.setInterID(rs.getString(5));
                mannger.setInterName(rs.getString(6));
                mannger.setSenior(rs.getInt(7));
                list.add(mannger);

            }
        } catch (Exception e1) {
            result.setStatus("-1");
            result.setMessage(e1.getMessage());
            return result;
        }
        result.setStatus("1");
        result.setMessage(list);
        return result;

    }
    public String justgetID() throws SQLException {
        String id = null;
        sql = "SELECT ID FROM   Mannger order by (ID+0) DESC limit 1";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            rs = cmmd.executeQuery();
            while (rs.next()){
                id = rs.getString(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
    }
    public Manger get_byID(String id){
        Manger manger = new Manger();
        sql = "Select * from Mannger where ID = '" + id + "'";
        try {
            cmmd = new util.Java_to_MySql().con().prepareStatement(sql);
            System.out.println(cmmd);
            rs = cmmd.executeQuery(); //把数据加载到数据集
            while (rs.next()) {
                manger.setID(rs.getString(1));
                manger.setName(rs.getString(2));
                manger.setAccount(rs.getString(3));
                manger.setPassword(rs.getString(4));
                manger.setInterID(rs.getString(5));
                manger.setInterName(rs.getString(6));
                manger.setSenior(rs.getInt(7));

            }
        } catch (Exception e1) {
            JOptionPane.showMessageDialog(null,"没有获取到信息");
        }
        return manger;
    }


}
